package com.study.manage.redis;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.study.manage.dal.EvaluateApplyRepository;
import com.study.manage.dal.EvaluateApplyWorkRepository;
import com.study.manage.dal.EvaluatePriceRepository;
import com.study.manage.dal.OrdersRepository;
import com.study.manage.dal.UserDetailRepository;
import com.study.manage.dal.UserRepository;
import com.study.manage.domain.EvaluateApply;
import com.study.manage.domain.EvaluateApplyWork;
import com.study.manage.domain.EvaluatePrice;
import com.study.manage.domain.Orders;
import com.study.manage.domain.User;
import com.study.manage.domain.UserDetail;
import com.study.manage.domain.enumm.LiveDieState;
import com.study.manage.domain.enumm.OrdersType;
import com.study.manage.domain.enumm.PayState;

@Component
public class RedisRepository {

    @Autowired
    private EvaluateApplyRepository evaluateApplyRepository;
    @Autowired
    private EvaluateApplyWorkRepository evaluateApplyWorkRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserDetailRepository userDetailRepository;
    @Autowired
    private OrdersRepository ordersRepository;
    @Autowired
    private EvaluatePriceRepository evaluatePriceRepository;

    public List<Object> excelApplyById(String id) {
        EvaluateApply apply = evaluateApplyRepository.findOne(Long.valueOf(id));
        User user = userRepository.findOne(apply.userId);
        User ref = null;
        String refName = "";
        String realName = "";
        if (apply.referenceId != null) {
            ref = userRepository.findById(apply.referenceId);
            if (ref != null) {
                UserDetail refUd = userDetailRepository.findByUserId(ref.id);
                refName = ref.name;
                realName = refUd.realName;
            }

        }

        PayState payState = PayState.DZF;
        Float payMoney = 0f;
        List<Orders> os = ordersRepository.findByTypeAndExtId(OrdersType.BSBM, apply.id);
        String orderNo = "--";
        if (!os.isEmpty()) {
            payState = os.get(0).payState;
            payMoney = os.get(0).total;
            orderNo = os.get(0).orderNo;
        }
        String epString = "";
        if (apply.evaluatePriceId != null) {
            EvaluatePrice ep = evaluatePriceRepository.findOne(apply.evaluatePriceId);
            if (ep != null) {
                epString = new StringBuffer().append(ep.title).append("(").append(ep.charge)
                        .append(")").toString();
            }
        }

        List<Object> arra = new ArrayList<>();
        arra.add(DateFormatUtils.format(apply.createdAt, "yyyy.MM.dd"));
        List<EvaluateApplyWork> eaws = evaluateApplyWorkRepository
                .findByEvaluateApplyIdAndUserIdAndState(apply.id, apply.userId, LiveDieState.LIVE);
        if (eaws.isEmpty()) {
            arra.add("--");
        } else {
            arra.add(DateFormatUtils.format(eaws.get(0).createdAt, "yyyy.MM.dd"));
        }
        arra.add(apply.number);
        arra.add(user.name);
        arra.add(user.number);
        arra.add(apply.type.desc);
        arra.add(apply.itemName == null ? "" : apply.itemName);
        arra.add(apply.fullName == null ? "" : apply.fullName);
        arra.add(apply.phone == null ? "" : apply.phone);
        arra.add(apply.groupName == null ? "" : apply.groupName);
        arra.add(refName);
        arra.add(realName);
        arra.add(apply.verify.desc);
        arra.add(apply.verifyState.desc);
        arra.add(apply.state.desc);
        arra.add(payState.desc);
        arra.add(orderNo);
        arra.add(epString);
        arra.add(payMoney);
        arra.add(eaws.size() <= 0 ? "无" : "有");
        arra.add(apply.remark);
        arra.add(apply.advName);
        return arra;
    }

}
